package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entry.Message;
import com.example.demo.entry.User;
import com.example.demo.mapper.MessageMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.utils.JwtUtils;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.jsonwebtoken.Jwts;
import javax.jws.soap.SOAPBinding;
import java.util.List;

@RestController

public class userController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageMapper messageMapper;
    @GetMapping("/getOneUserInfo")
    public List<User>  getOneUserInfo(String username)
    {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        List<User>  userList = userMapper.selectList(userQueryWrapper);
        return userList;
    }
    @GetMapping("/getAllUserInfo")
    public List<User> getAllUserInfo()
    {
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
        return userList;
    }
    @PostMapping("/insertUserInfo")
    public int insertUserInfo(@RequestBody  User user)
    {
        return userMapper.insert(user);
    }
    @PostMapping("/deleteUserInfo")
    public int deleteUserInfo(String username)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.delete(queryWrapper);
    }
    @PostMapping("/updateUserInfo")
    public int updateUserInfo(@RequestBody User user)
    {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("username",user.username);
        return userMapper.update(user,userUpdateWrapper);
    }
    @GetMapping("/getAllUserInfoAndMessage")
    public List<User> getUserInfoAndMessage()
    {
        return userMapper.selectAllUserAndMessages();
    }
    @GetMapping("/getOneUserInfoAndMessage")
    public List<User> getOneUserInfoAndMessage(String username)
    {
        return userMapper.selectOneUserAndMessages(username);
    }

    @GetMapping("/getUserInfoByPage")
    public IPage getUserInfoByPage(int startPage,int size) {
        User user = null;
        int i;
        Page<User> page = new Page<>(startPage, size);
        IPage iPage = userMapper.selectPage(page, null);
        List<User> userList = iPage.getRecords();
        QueryWrapper<Message> messageQueryWrapper = new QueryWrapper<>();

        for (i = 0; i < userList.size(); i++)
        {
            user = userList.get(i);
            messageQueryWrapper.eq("username", user.username);
            List<Message> messageList = messageMapper.selectList(messageQueryWrapper);
            user.setMessages(messageList);
            userList.set(i, user);
        }
        iPage.setRecords(userList);
        return iPage;
    }


}
